import {
	baseURL
} from './env.js'

// 需要修改token，和根据实际修改请求头
export default (url, method, params, isJson) => {
	// 获取本地token
	const token = uni.getStorageSync('token');
	// const systemAppId = uni.getStorageSync('systemAppId');
	//判断进入页面的时候或者本地有没有token,有就直接值某一页，没有则请求接口
	// if (token || url == "api/User/Login") {
	// if (url != 'api/EnergyCalculation/GetReportImg' && url != 'api/EnergyCalculation/GetThreeModelImg' && url !=
	// 	'api/EnergyCalculation/DepthReportCalculation') {
	uni.showLoading({
		title: "加载中"
	});
	// }
	// 根据请求方法或URL来判断是否添加请求头
	let header = {}
	if (token) {
		header['Authorization'] = `Bearer ${token}`
	}
	header['content-type'] = isJson ? "application/json; charset=utf-8" : "application/x-www-form-urlencoded"
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseURL + url,
			method: method,
			header: header,
			data: params,
			success(response) {
				// console.log('response', response)
				uni.hideLoading();
				const res = response
				// 根据返回的状态码做出对应的操作
				if (res.statusCode === 200) {
					if (res.data.code === 0) {
						resolve(res.data);
					} else if (res.data.code === 401) {
						uni.reLaunch({
							url: '/pages/login/index'
						})
						reject(new Error())
					} else {
						reject(new Error())
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 6000
						});
					}
				} else {
					reject(new Error())
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 6000
					});
				}
			},
			fail(err) {
				uni.hideLoading();
				reject(err);
			}
		});
	}).catch((e) => {})
};